package com.zzyl.serve.service.impl;

import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
  @Autowired
  private NursingProjectPlanMapper nursingProjectPlanMapper;

  @Autowired
  private RedisTemplate<Object, Object> nursingPlanRedisTemplate;

  private static  final String PLAN_KEY = "plan: all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
       // return getById(id);
        // 1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);

        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        //1.在缓存中查询数据
        List<NursingPlan> nursingPlans = (List<NursingPlan>) nursingPlanRedisTemplate.opsForValue().get(PLAN_KEY);
        //2.判断缓存中是否有数据,有就直接返回，没有就查询数据库，然后添加到缓存中
        if (nursingPlans != null && nursingPlans.size() > 0) {
            return nursingPlans;
        }
        //3.查询数据库
        nursingPlans = nursingPlanMapper.selectNursingPlanList(nursingPlan);
        //4.将查到的数据添加到缓存中
        nursingPlanRedisTemplate.opsForValue().set(PLAN_KEY, nursingPlans);
        return nursingPlans;
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        //保存护理计划
        //拷贝属性
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

       //批量保存护理项目计划关系
      long count= nursingProjectPlanMapper.batchInsert(nursingPlanDto.getProjectPlans(), nursingPlan.getId());
        //删除缓存
        deleteCache();
        return count>0 ? 1 : 0;
    }

    private void deleteCache() {
        nursingPlanRedisTemplate.delete(PLAN_KEY);
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        try {
            // 属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(dto, nursingPlan);

            // 判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
            if (dto.getProjectPlans() != null || !dto.getProjectPlans().isEmpty()) {
                // 删除护理计划对应的护理项目列表
                nursingProjectPlanMapper.deleteByPlanId(nursingPlan.getId());
                // 批量保存护理计划对应的护理项目列表
                nursingProjectPlanMapper.batchInsert(dto.getProjectPlans(), nursingPlan.getId());
            }

            // 不管项目列表是否为空，都要修改护理计划
            boolean flag = updateById(nursingPlan);
            //删除缓存
            deleteCache();
            return flag ? 1 : 0;
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean flag = removeBatchByIds(Arrays.asList(ids));
        //删除缓存
        deleteCache();

        return flag ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Long id)
    {       nursingProjectPlanMapper.deleteByPlanId(id);

        boolean flag = removeById(id);
        //删除缓存
        deleteCache();
        return flag ? 1 : 0;
    }
}
